import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    name: 'index',
    meta:{title:'首页',show:true},
    component: ()=>import('../views/index.vue'),
    children:[
      {
        path: '/index/home',
        name: 'index',
        meta:{title:'首页',show:true},
        component: ()=>import('../views/second/Home.vue'),
      },
      {
        path: '/index/tuan',
        name: 'tuan',
        meta:{title:'爆爆团',show:true},
        component: ()=>import('../views/second/Tuan.vue'),
        children:[
          {
            path: '/index/tuan/list1',
            name: 'baolist1',
            meta:{title:'正在抢购',show:true},
            component: ()=>import('../views/second/third/TuanList1.vue'),
          },
          {
            path: '/index/tuan/list2',
            name: 'baolist2',
            meta:{title:'上新预告',show:true},
            component: ()=>import('../views/second/third/TuanList2.vue'),
          },
          {
            path:'/index/tuan',
            redirect:'/index/tuan/list1'
          }
        ]
      },
      {
        path: '/index/order',
        name: 'order',
        meta:{title:'订单',show:true},
        component: ()=>import('../views/second/Order.vue'),
      },
      {
        path: '/index/mine',
        name: 'mine',
        meta:{title:'我的',show:true},
        component: ()=>import('../views/second/Mine.vue'),
      },
      {
        path:'/index',
        redirect:'/index/home',
      },
      {
        path:'*',
        component:()=>import('../views/NotFound.vue')
      }
    ]
  },
  {
    path: '/address',
    name: 'address',
    meta:{title:'地址',show:true},
    component: ()=>import('../views/Address.vue'),
      
  },
  {
    path: '/ressdetails',
    name: 'ressdetails',
    meta:{title:'我的详情地址',show:true},
    component: ()=>import('../views/Ressdetails.vue'),
  },
  {
    path: '/addaddress',
    name: 'addaddress',
    meta:{title:'新增地址',show:true},
    component: ()=>import('../views/addAddress.vue'),
  },
  {
    path: '/login',
    name: 'login',
    meta:{title:'登陆',show:true},
    component: ()=>import('../views/Login.vue'),
  },
  {
    path: '/register',
    name: 'register',
    meta:{title:'注册',show:true},
    component: ()=>import('../views/Register.vue'),
  },
  {
    path: '/search',
    name: 'search',
    meta:{title:'搜索',show:true},
    component: ()=>import('../views/Search.vue'),
    children:[
      {
        path: '/search/history',
        name: 'history',
        component: ()=>import('../views/second/History.vue'),
      },
      {
        path: '/search/searchlist',
        name: 'searchlist',
        component: ()=>import('../views/second/SearchList.vue'),
      },
      {
        path:'/search',
        redirect:'/search/history',
      },
      {
        path:'*',
        component:()=>import('../views/NotFound.vue')
      }
    ]
  },
  {
    path: '/collect',
    name: 'collect',
    meta:{title:'我的收藏',show:true},
    component: ()=>import('../views/Collect.vue'),
  },
  {
    path: '/detail/:id',
    name: 'detail',
    meta:{title:'详情页',show:true},
    component: ()=>import('../views/Detail.vue'),
  },
  {
    path:'/',
    redirect:'/index',
  },
  {
    path:'*',
    component:()=>import('../views/NotFound.vue')
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  document.title=to.meta.title;
  var token = localStorage.getItem('token');
  if(to.path=='/index/mine'||to.path=='/index/order'){
    if(token){
      next();
    }else{
      next('/login');
    }
  }
  next();
})

export default router
